package logic

import (
	"context"
	"fmt"
	"tiktok/models/mysql"
	"tiktok/pkg"

	"tiktok/tiktokrpc/internal/svc"
	"tiktok/tiktokrpc/tiktokrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthLogic {
	return &AuthLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 实名认证
func (l *AuthLogic) Auth(in *tiktokrpc.AuthRequest) (*tiktokrpc.AuthResponse, error) {
	// todo: add your logic here and delete this line
	auths := mysql.AuthRealName{
		UserId: int(in.UserId),
		Name:   in.Name,
		IdCard: in.IdCard,
	}
	var err error
	if err = auths.RealNameList(); err != nil {
		logx.Errorf("实名认证不存在")
		return nil, fmt.Errorf("实名认证不存在")
	}
	if auths.ID > 0 {
		logx.Errorf("用户已实名")
		return nil, fmt.Errorf("用户已实名")
	}
	if !pkg.AuthRealName(in.IdCard, in.Name) {
		logx.Errorf("实名认证失败")
		return nil, fmt.Errorf("实名认证失败")
	}
	if err = auths.RealNameAdd(); err != nil {
		logx.Errorf("实名认证创建失败")
		return nil, fmt.Errorf("实名认证创建失败")
	}
	return &tiktokrpc.AuthResponse{}, nil
}
